/* $RCSFile: SecureImagePage.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ImageSettings/UI/SecureImagePage.cs $
 * $log$
 * Revision 7 2010/08/09 09:44:02  christian.surieux
 *   Added Log to cs sources files
 */

using System;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.UI;
using AddonNice.Diagnostics;
using AddonNice.UI.WebControls;
using AddonNice.Security;
using AddonNice.Configuration;

using AddonNice.CRMImage;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.CRMImage.UI
{
	/// <summary>
    /// SecureImagePage inherits from AddEditImagePage
	/// To be inherited
	/// </summary>
	public class SecureImagePage : AddEditImagePage
    {
		/// <summary>
		/// Get the AllowedModules array from page if exists and set the restrictions for use
		/// For this method work, the user page need override AllowedModules with GUIDS
		/// </summary>
		protected override void ModuleGuidInCookie () 
		{
			if (AllowedModules != null) 
			{
				string guidsInUse = string.Empty;
				if (base.Request.Cookies[StStr.strSecurityCookieName] != null)
				{
					guidsInUse = Request.Cookies[StStr.strSecurityCookieName].Value;
				}
				foreach (string mg in AllowedModules) 
				{
					if (guidsInUse.IndexOf(mg.ToUpper()) > -1) 
                        return;
				}
				if (ModuleID != 0)
				{
					guidsInUse = (new ModulesDB()).GetModuleGuid(ModuleID).ToString().ToUpper();
					foreach (string mg in AllowedModules) 
					{
                        if ( PageTraceSwitch.Sw.TraceVerbose )
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]SecureImagePage.ModuleGuidInCookie ModuleID : {0}, guidsInUse : '{1}', mg: '{2}'", ModuleID, guidsInUse,mg),PageTraceSwitch.Sw.Info);
						if (guidsInUse.IndexOf(mg.ToUpper()) > -1) 
                            return;
					}
				}
                if ( PageTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]SecureImagePage.ModuleGuidInCookie AccessDenied ModuleID : {0}, guidsInUse : {1}", ModuleID, guidsInUse),PageTraceSwitch.Sw.Info);
				PortalSecurity.AccessDenied();
			}
		}

	}
}